package vsm.vsm_server.controller;

import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vsm.vsm_server.pojo.Image.Image;
import org.springframework.data.redis.core.StringRedisTemplate;
import vsm.vsm_server.pojo.Image.ImageInfo;
import vsm.vsm_server.pojo.Tag;
import vsm.vsm_server.pojo.Video.Video;
import vsm.vsm_server.pojo.Video.VideoCreateDTO;
import vsm.vsm_server.pojo.VodCallBack.ImageUpCallBack;
import vsm.vsm_server.pojo.VodCallBack.VideoUpCallBack;
import vsm.vsm_server.service.ImageService;
import vsm.vsm_server.service.TagService;
import vsm.vsm_server.service.VideoService;

import java.math.BigInteger;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/callback")
@CrossOrigin
public class CallbackController {

    @Autowired
    ImageService imageService;
    @Autowired
    VideoService videoService;
    @Autowired
    TagService tagService;
    @Autowired
    private StringRedisTemplate redisTemplate; // 注入 Redis 模板

    @PostMapping("/videoup")
    public void videoUp(@RequestBody String s){
        try {
            log.info("request:{}", s);
            VideoUpCallBack videoUpCallBack = JSON.parseObject(s, VideoUpCallBack.class);
            String status = videoUpCallBack.getStatus();
            String videoId = videoUpCallBack.getVideoId();
            log.info("callback:{}",videoUpCallBack);
            if(Objects.equals(status, "success")){
                String redisKey = "video_upload_info:" + videoId;
                String up = redisTemplate.opsForValue().get(redisKey);
                Video video = new Video();
                VideoCreateDTO videoCreateDTO = JSON.parseObject(up, VideoCreateDTO.class);
                BeanUtils.copyProperties(videoCreateDTO, video);
                video.setPath(videoId);
                video.setViewCount(0);
                video.setLikeCount(0);
                video.setCommentId(BigInteger.valueOf(-1)); //暂时以-1代替
                /** ？？？
                 //加入审核表，获取 videoCreateDTO.getAccess()，先默认access自动设为 -1，等审核通过再将其改为保存的 videoCreateDTO.getAccess()
                 newvideo.setAccess(videoCreateDTO.getAccess())
                 ;*/

                String content = videoCreateDTO.getTagContent();
                Tag tag = new Tag();
                if(content!=null){
                   BigInteger tagId =  tagService.saveTag(content);
                   log.info("tagId:{}",tagId);
                   video.setTagId(tagId);
                }
                videoService.createVideo(video);
                log.info("成功上传视频：" +videoId);
            }else{
                log.error("上传视频失败：" + videoId);
            }
            redisTemplate.delete(videoId);
            log.info("清除缓存：" + videoId);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @PostMapping("/imageup")
    public void imageUp(@RequestBody ImageUpCallBack imageUpCallBack){

         try {
             String status = imageUpCallBack.getStatus();
             String imageId = imageUpCallBack.getImageId();
             if(Objects.equals(status, "success")){
                 String redisKey = "image_upload_info:" + imageId;
                 String up = redisTemplate.opsForValue().get(redisKey);
                 Image image = new Image();
                 //将图片放入数据库中
                 ImageInfo imageInfo = JSON.parseObject(up, ImageInfo.class);
                 BeanUtils.copyProperties(imageInfo, image);
                 image.setPath(imageId);
                 imageService.createImage(image);
                 log.info("成功上传图片：{}",imageId);
             }else{
                 log.error("失败上传图片：{}", imageId);
             }
             redisTemplate.delete(imageId);
             log.info("清除缓存：{}",imageId);
        }catch (Exception e) {
             throw new RuntimeException(e);
         }
    }
}
